#!/bin/bash
# Run a scenario with xfs_repair that bashes the VM in various different ways.
# The biggest factor is the amount it smacks the shrinker. Note that there may
# be different results based on the filesystem storing the disk image

###SHELLPACK preamble xfsrepair-bench 1

FSMARK_VERSION=3.3
ITERATIONS=3
NR_THREADS=4

###SHELLPACK parseargBegin
###SHELLPACK parseargInstall
###SHELLPACK parseargParam --iterations	ITERATIONS
###SHELLPACK parseargParam --threads    NR_THREADS
###SHELLPACK parseargEnd
###SHELLPACK monitor_hooks

install-depends xfsprogs
###SHELLPACK check_external_install_required fsmark fsmark-$FSMARK_VERSION $FSMARK_VERSION
###SHELLPACK init_complete

export PATH=$SHELLPACK_SOURCES/fsmark-${FSMARK_VERSION}-installed:$PATH

ln -s $SHELLPACK_DATA /tmp/fsmark-$$
IMAGE_SIZE=`df -k $SHELLPACK_DATA | tail -1 | awk '{print $2}'`
IMAGE_SIZE=$((IMAGE_SIZE*9/10))

# fsmark directory parameters
NR_SUB_DIRECTORIES=10000
NR_INODES=50000000
NR_FILES_ITERATION=500000
FSMARK_FILES=$(($NR_FILES_ITERATION/$NR_THREADS))
FSMARK_ITERATIONS=$((NR_INODES/NR_FILES_ITERATION))
PARAM=
for THREAD in `seq 1 $NR_THREADS`; do
	PARAM="$PARAM -d /tmp/fsmark-$$/mnt/$THREAD"
done

echo "Parameters"
echo "o nr_inodes         $NR_INODES"
echo "o nr_threads        $NR_THREADS"
echo "o nr_sub_dirs       $NR_SUB_DIRECTORIES"
echo "o total_files_iter  $NR_FILES_ITERATION"
echo "o fsmark_files_iter $FSMARK_FILES"
echo "o fsmark_iterations $FSMARK_ITERATIONS"

###SHELLPACK iteration_begin $ITERATIONS
echo Creating sparse file size $((IMAGE_SIZE/1024))MB $ITERATION/$ITERATIONS 
cd $SHELLPACK_DATA
$TIME_CMD -o $LOGDIR_RESULTS/time.sparsecreate.$ITERATION \
	xfs_io -f -c "truncate ${IMAGE_SIZE}k" -c "extsize 1m" $SHELLPACK_DATA/sparse.img || \
		die Failed to create sparse file
xfs_io -c stat $SHELLPACK_DATA/sparse.img | tee $LOGDIR_RESULTS/sparse.stat

echo Creating filesystem image $ITERATION/$ITERATIONS
$TIME_CMD -o $LOGDIR_RESULTS/time.fscreate.$ITERATION \
	mkfs.xfs -f -l size=131072b $SHELLPACK_DATA/sparse.img || \
		die Failed to create filesystem

echo Mounting $ITERATION/$ITERATIONS
mkdir mnt
mount -o loop,nobarrier,logbsize=256k $SHELLPACK_DATA/sparse.img mnt
if [ $? -ne 0 ]; then
	mount -o loop,logbsize=256k $SHELLPACK_DATA/sparse.img mnt ||
		die Failed to mount filesystem image
fi

echo Running fsmark $ITERATION/$ITERATIONS
monitor_pre_hook $LOGDIR_RESULTS $ITERATION
eval save_rc time -o $LOGDIR_RESULTS/time.fsmark.$ITERATION \
	fs_mark  -D $NR_SUB_DIRECTORIES -S0 -n $FSMARK_FILES -s  0  -L $FSMARK_ITERATIONS $PARAM \
		2>&1 | tee $SHELLPACK_TEMP/fsmark.${ITERATION}.log
monitor_post_hook $LOGDIR_RESULTS $ITERATION
umount mnt
recover_rc
if [ $? -ne 0 ]; then
	die fs_mark failed
fi

echo Repairing filesystem $ITERATION/$ITERATIONS
echo 3 > /proc/sys/vm/drop_caches
monitor_pre_hook $LOGDIR_RESULTS xfsrepair-$ITERATION
save_rc $TIME_CMD -o $LOGDIR_RESULTS/time.xfsrepair.$ITERATION \
	xfs_repair -v -v -t 1 -o bhash=32768 -o ag_stride=$NR_THREADS $SHELLPACK_DATA/sparse.img \
		2>&1 | tee $SHELLPACK_TEMP/xfsrepair.${ITERATION}.log
monitor_post_hook $LOGDIR_RESULTS xfsrepair-$ITERATION
recover_rc
if [ $? -ne 0 ]; then
	die xfsrepair failed
fi

rm $SHELLPACK_DATA/sparse.img
###SHELLPACK iteration_end

rm /tmp/fsmark-$$

exit $SHELLPACK_SUCCESS
